package com.netease.nie.facade;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.CacheParam;
import com.netease.nie.base.cache.DataOperation;
import com.netease.nie.base.cache.redis.RedisManager;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.CustomDateFormatUtils;
import com.netease.nie.base.utils.DateUtils;
import com.netease.nie.common.CommonBusinessTools;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.CreditHistoryChangeStants;
import com.netease.nie.constants.CreditHistoryOriginStants;
import com.netease.nie.constants.PublishedConstants;
import com.netease.nie.constants.RobDealStatusConstatns;
import com.netease.nie.dto.GoodsRobParamDto;
import com.netease.nie.dto.GoodsRobRequestDto;
import com.netease.nie.dto.GoodsRobResponseDto;
import com.netease.nie.dto.RobGoodsCurrPeriodConfigDto;
import com.netease.nie.dto.RobGoodsPeriodPersonTimeDto;
import com.netease.nie.dto.RobGoodsPeriodUserPersonTimeDto;
import com.netease.nie.dto.RobGoodsTransactionalDto;
import com.netease.nie.model.Credit;
import com.netease.nie.model.CreditHistory;
import com.netease.nie.model.RobGoodsInfoPeriod;
import com.netease.nie.model.RobGoodsPeriodPersonTime;
import com.netease.nie.model.User;
import com.netease.nie.service.CreditService;
import com.netease.nie.service.GoodsRobService;
import com.netease.nie.service.RobGoodsCurrPeriodInfoConfigService;
import com.netease.nie.service.UserInfoService;

@Service
public class GoodsRobFacadeImpl extends CommonBusinessTools implements
		GoodsRobFacade {
	
	private static Logger logger = Logger.getLogger(GoodsRobFacadeImpl.class);
	
	private static final String ROB_GOODS_CONFIG_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_CONFIG_PREFIX;
	
	private static final String ROB_GOODS_PERIOD_REMAIN_PT_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_PERIOD_REMAIN_PT_PREFIX;
	
	private static final String ROB_GOODS_PERIOD_LOTTERY_NUMBER_LIST_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_PERIOD_LOTTERY_NUMBER_LIST_PREFIX;
	
	private static final String ROB_GOODS_PERIOD_ALLIN_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_PERIOD_ALLIN_PREFIX;
	
	private static final String ROB_GOODS_PERIOD_USER_PT_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_PERIOD_USER_PT_PREFIX;
	
	private static final String ROB_GOODS_DISPLAYPERIOD_PT_LIST_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_DISPLAYPERIOD_PT_LIST_PREFIX;
	
	private static final String ROB_GOODS_ID_DISPLAYPERIOD_PREFIX = CachePrefixConstants.RobGoodsPrefix.ROB_GOODS_ID_DISPLAYPERIOD_PREFIX;
	
	private static final String USER_CREDIT_PREFIX = CachePrefixConstants.UserPrefix.USER_CREDIT_PREFIX;
	
	@Autowired
	private RobGoodsCurrPeriodInfoConfigService robGoodsCurrPeriodInfoConfigService;
	
	@Autowired
	private UserInfoService userInfoService;
	
	@Autowired
	private CreditService creditService;
	
	@Autowired
	private GoodsRobService goodsRobService;

	public GoodsRobResponseDto goodsRob(GoodsRobRequestDto goodsRobRequestDto) {
		GoodsRobParamDto goodsRobParamDto = new GoodsRobParamDto();
		final int robGoodsId = goodsRobRequestDto.getRobGoodsId();
		final int displayPeriod = goodsRobRequestDto.getDisplayPeriod();
		int pt = goodsRobRequestDto.getPt();
		Long userId = goodsRobRequestDto.getUserId();
		
		RobGoodsCurrPeriodConfigDto robGoodsCurrPeriodConfigDto = queryRobGoodsCurrPeriodConfigDtoByCondition(robGoodsId, displayPeriod);
		if (robGoodsCurrPeriodConfigDto == null)
			throw new BusinessException(BusinessConstants.GOODS_CONFIG_NOT_EXISTS.getCode(), "商品配置不存在");
		goodsRobParamDto.setRobGoodsCurrPeriodConfigDto(robGoodsCurrPeriodConfigDto);
		
		RobGoodsTransactionalDto robGoodsTransactionalDto = new RobGoodsTransactionalDto();
		Credit credit = creditService.queryUserCreditByUserId(userId);
		if (credit == null)
			throw new BusinessException(BusinessConstants.CREDIT_NOT_EXISTS.getCode(), "用户积分不存在");
		
		User user = userInfoService.queryUserByUserId(userId);
		if (user == null)
			throw new BusinessException(BusinessConstants.USER_NOT_EXISTS.getCode(), "用户不存在");
		
		goodsRobParamDto.setRobUserNickname(user.getNickName());
		goodsRobParamDto.setCredit(credit);
		validateRobGoodsBaseInfo(robGoodsCurrPeriodConfigDto);
		GoodsRobResponseDto goodsRobResponseDto = validateRobGoodsPeriodInfo(robGoodsCurrPeriodConfigDto);
		
		if (goodsRobResponseDto != null)
			return goodsRobResponseDto;
		validateUserLevel(credit, robGoodsCurrPeriodConfigDto);
		
		/*if (pt == -1) {
			//包尾
			lockRobGoodsPeriodRemainPt(displayPeriod, userId);
		}
		
		Long lockUserId = getRobGoodsPeriodRemainPtLockValue(displayPeriod);
		if (lockUserId != null) {
			if (!lockUserId.equals(userId))
				throw new BusinessException(BusinessConstants.GOODS_STOCK_EMPTY.getCode(), "剩余次数已被抢光");
			pt = getCurrentRemainPt(displayPeriod);
			if (pt <= 0)
				throw new BusinessException(BusinessConstants.GOODS_STOCK_EMPTY.getCode(), "剩余次数已被抢光");
		}*/
		
		if (pt > 100)
			throw new BusinessException(BusinessConstants.GOODS_PERIOD_PERSON_TIME_OVERLIMIT.getCode(), "单次夺宝最多100人次");
		
		goodsRobParamDto.setRobUserIp(goodsRobRequestDto.getIp());
		goodsRobParamDto.setPt(pt);
		computePrice(goodsRobParamDto);
		
		//扣除当前库存
		int remainPt = deductRobGoodsPeriodRemainPt(displayPeriod, pt);
		boolean robStatus = false;
		if (remainPt >= 0) {
			//验证用户积分
			try {
				validateUserCurrentCredit(goodsRobParamDto);
			} catch (Exception e) {
				raiseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod(), pt);
				//relaseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod());
				throw new BusinessException(BusinessConstants.CREDIT_NOT_ENOUGH.getCode(), "您的奇遇点不足");
			}
			List<Integer> lotteryNumberList = getRobGoodsPeriodLotteryNumberList(displayPeriod, pt);
			if (CollectionUtils.isEmpty(lotteryNumberList)) {
				raiseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod(), pt);
				addUserCurrentCredit(userId, goodsRobParamDto.getPrice());
				//relaseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod());
				throw new BusinessException(BusinessConstants.GOODS_PERIOD_REMAIN_PT_ERROR.getCode(), "参与号码出错");
			}
			
			RobGoodsInfoPeriod robGoodsInfoPeriod = generateRobGoodsInfoPeriod(goodsRobParamDto, remainPt);
			computeUserCredit(goodsRobParamDto);
			RobGoodsPeriodPersonTime robGoodsPeriodPersonTime = generateCreditHistoryRobGoodsPeriodPersonTime(goodsRobParamDto, lotteryNumberList);
			CreditHistory creditHistory = generateCreditHistory(goodsRobParamDto);
			robGoodsTransactionalDto.setRobGoodsInfoPeriod(robGoodsInfoPeriod);
			robGoodsTransactionalDto.setRobGoodsPeriodPersonTime(robGoodsPeriodPersonTime);
			robGoodsTransactionalDto.setCredit(credit);
			robGoodsTransactionalDto.setCreditHistory(creditHistory);
			robGoodsTransactionalDto.setRobGoodsInfoPeriod(robGoodsInfoPeriod);
			try {
				goodsRobService.updateGoodsRobStatus(robGoodsTransactionalDto);
				robStatus = true;
			} catch (Exception e) {
				raiseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod(), pt);
				raiseRobGoodsPeriodLotteryNumberList(robGoodsCurrPeriodConfigDto.getDisplayPeriod(), lotteryNumberList);
				addUserCurrentCredit(userId, goodsRobParamDto.getPrice());
			}
			
			goodsRobResponseDto = new GoodsRobResponseDto();
			goodsRobResponseDto.setDisplayPeriod(robGoodsInfoPeriod.getDisplayPeriod());
			goodsRobResponseDto.setGoodsTitle(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getRobTitle());
			goodsRobResponseDto.setRobGoodsId(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getRobGoodsId());
			goodsRobResponseDto.setRobUserNumber(robGoodsPeriodPersonTime.getRobUserNumber());
			goodsRobResponseDto.setGoodsId(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getGoodsId());
			if (robStatus) {
				//重置用户积分缓存
				reloadUserCredit(userId);
				//重置用户参与情况缓存
				reloadRobGoodsPeriodUserPtCache(userId, displayPeriod, robGoodsPeriodPersonTime);
				//重置参与情况缓存
				reloadRobGoodsPeriodPersonTimeRecordCache(displayPeriod, robGoodsPeriodPersonTime);
				//释放夺宝参与权限
				//relaseRobGoodsPeriodRemainPt(displayPeriod);
				if (robGoodsInfoPeriod.getPublished() != null && 
						robGoodsInfoPeriod.getPublished() == PublishedConstants.PREPARED_PUBLISHED.getCode())
					//重置夺宝商品配置
					reloadRobGoodsCurrPeriodConfigCache(displayPeriod);
				goodsRobResponseDto.setStatus(RobDealStatusConstatns.SUCCESS.getCode());
				return goodsRobResponseDto;
			}
			//释放夺宝参与权限
			//relaseRobGoodsPeriodRemainPt(displayPeriod);
			goodsRobResponseDto.setStatus(RobDealStatusConstatns.FAILURE_AND_NO_NEXT_PERIOD.getCode());
			return goodsRobResponseDto;
		}
		raiseRobGoodsPeriodRemainPt(robGoodsCurrPeriodConfigDto.getDisplayPeriod(), pt);
		//库存不足
		throw new BusinessException(BusinessConstants.GOODS_STOCK_EMPTY.getCode(), "剩余人次不足");
	}
	

	/**
	 * 查询夺宝商品配置
	 * @param goodsId
	 * @return
	 */
	private RobGoodsCurrPeriodConfigDto queryRobGoodsCurrPeriodConfigDtoByCondition(final int robGoodsId, final int displayPeriod) {
		DataOperation dataOperation = new DataOperation() {
			
			public Object operate() {
				RobGoodsCurrPeriodConfigDto gobGoodsCurrPeriodConfigDto = robGoodsCurrPeriodInfoConfigService.queryRobGoodsCurrPeriodConfigDtoByCondition(robGoodsId, displayPeriod);
				return gobGoodsCurrPeriodConfigDto;
			}
		};
		String robGoodsConfigKey = ROB_GOODS_CONFIG_PREFIX + displayPeriod;
		List<String> keyList = new ArrayList<>();
		keyList.add(robGoodsConfigKey);
		CacheParam cacheParam = new CacheParam();
		cacheParam.setKeyList(keyList);
		cacheParam.setPrefix(ROB_GOODS_CONFIG_PREFIX);
		RedisManager redisManager = new RedisManager(cacheParam, dataOperation);
		RobGoodsCurrPeriodConfigDto robGoodsCurrPeriodConfigDto = redisManager.passiveOperateCache(RobGoodsCurrPeriodConfigDto.class);
		return robGoodsCurrPeriodConfigDto;
	}
	
	private void validateRobGoodsBaseInfo(RobGoodsCurrPeriodConfigDto robGoodsConfigDto) {
		long currTimeMills = DateUtils.currentTimeInMills();
		Integer putOnStatus = robGoodsConfigDto.getPutOnStatus();
		Long startTime = robGoodsConfigDto.getStartTime();
		Long endTime = robGoodsConfigDto.getEndTime();
		if (putOnStatus == null)
			throw new BusinessException(BusinessConstants.GOODS_PUT_ON_STATUS_NOT_EXISTS.getCode(), "夺宝商品上架状态不存在");
		if (startTime == null)
			throw new BusinessException(BusinessConstants.GOODS_PUT_OFF_TIME_NOT_EXISTS.getCode(), "夺宝开始时间不存在");
		if (startTime > currTimeMills)
			throw new BusinessException(BusinessConstants.GOODS_HAD_BEEN_PUT_OFF.getCode(), "夺宝商品未开始");
		if (putOnStatus != 2 || endTime != null)
			throw new BusinessException(BusinessConstants.GOODS_HAD_BEEN_PUT_OFF.getCode(), "夺宝已结束下架");
	}
	
	private GoodsRobResponseDto validateRobGoodsPeriodInfo(RobGoodsCurrPeriodConfigDto robGoodsConfigDto) {
		int robGoodsId = robGoodsConfigDto.getRobGoodsId();
		int published = robGoodsConfigDto.getPublished();
		//判断当前期数
		if (published != PublishedConstants.NOT_PUBLISHED.getCode()) {
			int nextDisplayPeriod = getRobGoodsNextDisplayPeriod(robGoodsId);
			if (nextDisplayPeriod > robGoodsConfigDto.getDisplayPeriod()) {
				GoodsRobResponseDto goodsRobResponseDto = new GoodsRobResponseDto();
				goodsRobResponseDto.setDisplayPeriod(nextDisplayPeriod);
				goodsRobResponseDto.setRobGoodsId(robGoodsId);
				goodsRobResponseDto.setGoodsTitle(robGoodsConfigDto.getRobTitle());
				goodsRobResponseDto.setGoodsId(robGoodsConfigDto.getGoodsId());
				goodsRobResponseDto.setStatus(RobDealStatusConstatns.FAILURE_AND_NEXT_PERIOD.getCode());
				return goodsRobResponseDto;
			}
			throw new BusinessException(BusinessConstants.GOODS_STOCK_EMPTY.getCode(), "剩余次数已被抢光");
		}
		return null;
	}
	
	private void validateUserLevel(Credit credit, RobGoodsCurrPeriodConfigDto robGoodsConfigDto) {
		int level = credit.getLevel();
		if (level < robGoodsConfigDto.getRobObjectId())
			//夺宝等级不足
			throw new BusinessException(BusinessConstants.LEVLE_NOT_ENOUGH.getCode(), "您的会员等级不够哦");
	}
	
	private void validateUserCurrentCredit(GoodsRobParamDto goodsRobParamDto) {
		Credit credit = goodsRobParamDto.getCredit();
		int price = goodsRobParamDto.getPrice();
		int currentCredit = deductUserCurrentCredit(credit.getUserId(), price);
		if (currentCredit < 0) {
			addUserCurrentCredit(credit.getUserId(), price);
			//用户可用积分不足
			throw new BusinessException(BusinessConstants.CREDIT_NOT_ENOUGH.getCode(), "您的奇遇点不足");
		}
	}
	
	//获取当前剩余参与人次
	private int getCurrentRemainPt(int displayPeriod) {
		String robGoodsPeriodRemainPtKey = ROB_GOODS_PERIOD_REMAIN_PT_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		int remainPt = redisManager.getAtomIntegerValue(robGoodsPeriodRemainPtKey);
		return remainPt;
	}
	
	//锁定夺宝商品参与权限
	private boolean lockRobGoodsPeriodRemainPt(final int displayPeriod, final long userId) {
		String lockKey = ROB_GOODS_PERIOD_ALLIN_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		return redisManager.add(lockKey, String.valueOf(userId));
	}
	
	//释放夺宝商品参与权限
	private void relaseRobGoodsPeriodRemainPt(final int displayPeriod) {
		String lockKey = ROB_GOODS_PERIOD_ALLIN_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		redisManager.del(lockKey);
	}
	
	//获取夺宝商品参与权限
	private Long getRobGoodsPeriodRemainPtLockValue(final int displayPeriod) {
		String lockKey = ROB_GOODS_PERIOD_ALLIN_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		String lockValue = redisManager.getAtomStringValue(lockKey);
		if (StringUtils.isBlank(lockValue))
			return null;
		return Long.valueOf(lockValue);
	}
	
	//锁定夺宝商品参与人次
	private int deductRobGoodsPeriodRemainPt(final int displayPeriod, int pt) {
		String robGoodsPeriodRemainPtKey = ROB_GOODS_PERIOD_REMAIN_PT_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		int remainPt = redisManager.decrBy(robGoodsPeriodRemainPtKey, (long) pt);
		return remainPt;
	}
	
	//释放商品商品参与人次
	private int raiseRobGoodsPeriodRemainPt(final int displayPeriod, int pt) {
		String robGoodsPeriodRemainPtKey = ROB_GOODS_PERIOD_REMAIN_PT_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		int remainPt = redisManager.incrBy(robGoodsPeriodRemainPtKey, pt);
		return remainPt;
	}
	
	//计算用户当月可用积分和待过期积分
	private void computeUserCredit(GoodsRobParamDto goodsRobParamDto) {
		Credit credit = goodsRobParamDto.getCredit();
		credit.setReadyToExpireCredit(goodsRobParamDto.getPrice());
		credit.setCurrentCredit(goodsRobParamDto.getPrice());
		credit.setTotalUseCredit(goodsRobParamDto.getPrice());
	}
	
	private CreditHistory generateCreditHistory(GoodsRobParamDto goodsRobParamDto) {
		long currentTimeMills = DateUtils.currentTimeInMills();
		CreditHistory creditHistory = new CreditHistory();
		creditHistory.setChangeOriginStatus(CreditHistoryOriginStants.ROB.getCode());
		creditHistory.setChangeStatus(CreditHistoryChangeStants.DECR.getCode());
		creditHistory.setCreateTime(currentTimeMills);
		creditHistory.setCredit(goodsRobParamDto.getPrice());
		creditHistory.setUserId(goodsRobParamDto.getCredit().getUserId());
		creditHistory.setGenerateDate(CustomDateFormatUtils.getyyyyMMddInt(currentTimeMills));
		String changeOriginDesc = "参与" + goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getRobTitle() + "夺宝";
		creditHistory.setChangeOriginDesc(changeOriginDesc);
		return creditHistory;
	}
	
	//释放参与号码
	private void raiseRobGoodsPeriodLotteryNumberList(final int displayPeriod, List<Integer> lotteryNumberList) {
		String robGoodsPeriodLotteryNumberKey = ROB_GOODS_PERIOD_LOTTERY_NUMBER_LIST_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		redisManager.rpush(robGoodsPeriodLotteryNumberKey, lotteryNumberList);
	}
	
	//获取参与号码
	private List<Integer> getRobGoodsPeriodLotteryNumberList(final int displayPeriod, int pt) {
		String robGoodsPeriodLotteryNumberKey = ROB_GOODS_PERIOD_LOTTERY_NUMBER_LIST_PREFIX + displayPeriod;
		List<Integer> lotteryNumberList = new ArrayList<>();
		String lotteryNumberStr;
		RedisManager redisManager = new RedisManager();
		for (int i = 0; i < pt; i++) {
			lotteryNumberStr = redisManager.lpop(robGoodsPeriodLotteryNumberKey);
			if (StringUtils.isNotBlank(lotteryNumberStr)) {
				int lotteryNumber = Integer.valueOf(lotteryNumberStr);
				lotteryNumberList.add(lotteryNumber);
			}
		}
		return lotteryNumberList;
	}
	
	private RobGoodsPeriodPersonTime generateCreditHistoryRobGoodsPeriodPersonTime(GoodsRobParamDto goodsRobParamDto, List<Integer> robGoodsPeriodLotteryNumberList) {
		RobGoodsPeriodPersonTime robGoodsPeriodPersonTime = new RobGoodsPeriodPersonTime();
		long currTimeInMills = DateUtils.currentTimeInMills();
		robGoodsPeriodPersonTime.setCreateTime(currTimeInMills);
		robGoodsPeriodPersonTime.setDisplayPeriod(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getDisplayPeriod());
		robGoodsPeriodPersonTime.setPeriodId(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getPeriodId());
		robGoodsPeriodPersonTime.setPeriodStartTime(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getStartTime());
		robGoodsPeriodPersonTime.setRobUserId(goodsRobParamDto.getCredit().getUserId());
		robGoodsPeriodPersonTime.setRobUserIp(goodsRobParamDto.getRobUserIp());
		robGoodsPeriodPersonTime.setRobUserLevel(goodsRobParamDto.getCredit().getLevel());
		robGoodsPeriodPersonTime.setRobUserNickname(goodsRobParamDto.getRobUserNickname());
		robGoodsPeriodPersonTime.setRobUserPt(goodsRobParamDto.getPt());
		robGoodsPeriodPersonTime.setRobUserNumber(transRobUserNumber(robGoodsPeriodLotteryNumberList));
		return robGoodsPeriodPersonTime;
	}
	
	private RobGoodsInfoPeriod generateRobGoodsInfoPeriod(GoodsRobParamDto goodsRobParamDto, int remainPt) {
		RobGoodsInfoPeriod robGoodsInfoPeriod = new RobGoodsInfoPeriod();
		robGoodsInfoPeriod.setDisplayPeriod(goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getDisplayPeriod());
		robGoodsInfoPeriod.setRemainPt(goodsRobParamDto.getPt());
		if (remainPt == 0) {
			long currTimeInMills = DateUtils.currentTimeInMills();
			robGoodsInfoPeriod.setCutOffTime(currTimeInMills);
			robGoodsInfoPeriod.setPublished(PublishedConstants.PREPARED_PUBLISHED.getCode());
			long publishedTime = DateUtils.addSpecifyMinute(currTimeInMills, 3);
			robGoodsInfoPeriod.setPublishedTime(publishedTime);
		}
		
		return robGoodsInfoPeriod;
	}
	
	private String transRobUserNumber(List<Integer> robGoodsPeriodLotteryNumberList) {
		if (robGoodsPeriodLotteryNumberList != null && !robGoodsPeriodLotteryNumberList.isEmpty()) {
			int robGoodsPeriodLotteryNumberListSize = robGoodsPeriodLotteryNumberList.size();
			StringBuilder robUserNumberBuilder = new StringBuilder();
			for (int i = 0; i < robGoodsPeriodLotteryNumberListSize; i++)
				robUserNumberBuilder.append(robGoodsPeriodLotteryNumberList.get(i)).append(",");
			
			int robUserNumberBuilderLen = robUserNumberBuilder.length();
			if (robUserNumberBuilderLen > 0)
				robUserNumberBuilder.deleteCharAt(--robUserNumberBuilderLen);
			return robUserNumberBuilder.toString();
		}
		return null;
	}
	
	//重置用户积分缓存
	private void reloadUserCredit(final long userId) {
		creditService.reloadUserCredit(userId);
	}
	
	//重置用户参与情况缓存
	private void reloadRobGoodsPeriodUserPtCache(long userId, int displayPeriod, RobGoodsPeriodPersonTime robGoodsPeriodPersonTime) {
		RobGoodsPeriodUserPersonTimeDto robGoodsPeriodUserPersonTimeDto = new RobGoodsPeriodUserPersonTimeDto();
		CopyUtils.copy(robGoodsPeriodUserPersonTimeDto, robGoodsPeriodPersonTime);
		String key = ROB_GOODS_PERIOD_USER_PT_PREFIX + userId + "_" + displayPeriod;
		RedisManager redisManager = new RedisManager();
		redisManager.rpush(key, robGoodsPeriodUserPersonTimeDto);
	}
	
	//重置用户参与情况
	private void reloadRobGoodsPeriodPersonTimeRecordCache(int displayPeriod, RobGoodsPeriodPersonTime robGoodsPeriodPersonTime) {
		RobGoodsPeriodPersonTimeDto robGoodsPeriodPersonTimeDto = new RobGoodsPeriodPersonTimeDto();
		CopyUtils.copy(robGoodsPeriodPersonTimeDto, robGoodsPeriodPersonTime);
		String key = ROB_GOODS_DISPLAYPERIOD_PT_LIST_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		redisManager.lpush(key, robGoodsPeriodPersonTimeDto);
	}
	
	//重置夺宝商品配置
	private void reloadRobGoodsCurrPeriodConfigCache(int displayPeriod) {
		String key = ROB_GOODS_CONFIG_PREFIX + displayPeriod;
		RedisManager redisManager = new RedisManager();
		redisManager.del(key);
	}
	
	//获取当前夺宝商品下一期期数
	private Integer getRobGoodsNextDisplayPeriod(int robGoodsId) {
		String key = ROB_GOODS_ID_DISPLAYPERIOD_PREFIX + robGoodsId;
		RedisManager redisManager = new RedisManager();
		String robGoodsNextDisplayPeriod = redisManager.getAtomStringValue(key);
		if (StringUtils.isNotBlank(robGoodsNextDisplayPeriod))
			return Integer.valueOf(robGoodsNextDisplayPeriod);
		return null;
	}
	
	//尝试扣减用户积分
	private int deductUserCurrentCredit(long userId, int price) {
		String key = USER_CREDIT_PREFIX + userId;
		RedisManager redisManager = new RedisManager();
		Long currentCredit = redisManager.hincrBy(key, "currentCredit", -price);
		return currentCredit.intValue();
	}
	
	//尝试归还用户积分
	private void addUserCurrentCredit(long userId, int price) {
		String key = USER_CREDIT_PREFIX + userId;
		RedisManager redisManager = new RedisManager();
		redisManager.hincrBy(key, "currentCredit", price);
	}
	
	
	private void computePrice(GoodsRobParamDto goodsRobParamDto) {
		if (goodsRobParamDto != null) {
			if (goodsRobParamDto.getPt() != null) {
				int eachPrice = goodsRobParamDto.getRobGoodsCurrPeriodConfigDto().getEachPrice();
				int pt = goodsRobParamDto.getPt();
				goodsRobParamDto.setPrice(eachPrice * pt);
			}
		}
	}

}
